package com.lefevre.cms.web.action.like;

import com.lefevre.cms.bean.*;
import com.lefevre.cms.bean.like.Like;
import com.lefevre.cms.bean.topic.Topic;
import com.lefevre.cms.bean.user.User;
import com.lefevre.cms.service.like.LikeService;
import com.lefevre.cms.service.setting.SettingService;
import com.lefevre.cms.service.topic.TopicService;
import com.lefevre.cms.service.user.UserService;
import com.lefevre.cms.utils.JsonUtils;
import com.lefevre.cms.web.action.fileSystem.FileManage;
import com.lefevre.cms.web.action.user.UserManage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 点赞
 *
 * @author Lefevre
 */
@Controller
public class LikeController {

    @Resource
    LikeService likeService;
    @Resource
    SettingService settingService;
    @Resource
    TopicService topicService;
    @Resource
    UserManage userManage;
    @Resource
    UserService userService;
    @Resource
    FileManage fileManage;

    /**
     * 点赞列表
     *
     * @param id       用户Id
     * @param userName 用户名称
     */
    @ResponseBody
    @RequestMapping("/control/like/list")
    public String likeList(PageForm pageForm, @RequestParam Long id, String userName, HttpServletRequest request) throws Exception {
        Map<String, String> error = new HashMap<>();
        Map<String, Object> returnValue = new HashMap<>();
        if (id != null && id > 0L && userName != null && !"".equals(userName.trim())) {
            PageView<Like> pageView = new PageView<>(settingService.findSystemSetting_cache().getBackstagePageNumber(), pageForm.getPage(), 10, request.getRequestURI(), request.getQueryString());
            int firstIndex = (pageForm.getPage() - 1) * pageView.getMaxresult();

            QueryResult<Like> qr = likeService.findLikeByUserId(id, userName, firstIndex, pageView.getMaxresult());
            if (qr != null && qr.getResultlist() != null && qr.getResultlist().size() > 0) {
                List<Long> topicIdList = new ArrayList<>();
                for (Like like : qr.getResultlist()) {
                    topicIdList.add(like.getTopicId());
                }
                List<Topic> topicList = topicService.findByIdList(topicIdList);
                if (topicList != null && topicList.size() > 0) {
                    for (Like like : qr.getResultlist()) {
                        for (Topic topic : topicList) {
                            if (like.getTopicId().equals(topic.getId())) {
                                like.setTopicTitle(topic.getTitle());
                                break;
                            }
                        }
                    }
                }
            }
            //将查询结果集传给分页List
            pageView.setQueryResult(qr);
            User user = userService.findUserById(id);
            if (user != null) {
                returnValue.put("currentUser", user);
            }
            returnValue.put("pageView", pageView);
        } else {
            error.put("userId", "用户Id或用户名称不能为空");
        }
        if (error.size() > 0) {
            return JsonUtils.toJSONString(new RequestResult(ResultCode.FAILURE, error));
        } else {
            return JsonUtils.toJSONString(new RequestResult(ResultCode.SUCCESS, returnValue));
        }
    }


    /**
     * 话题点赞列表
     *
     * @param topicId 话题Id
     */
    @ResponseBody
    @RequestMapping("/control/topicLike/list")
    public String topicLikeList(PageForm pageForm, Long topicId, HttpServletRequest request) throws Exception {
        Map<String, String> error = new HashMap<>();
        Map<String, Object> returnValue = new HashMap<>();
        if (topicId != null && topicId > 0L) {
            Topic topic = topicService.findById(topicId);
            if (topic != null) {
                returnValue.put("currentTopic", topic);
            }
            PageView<Like> pageView = new PageView<>(settingService.findSystemSetting_cache().getBackstagePageNumber(), pageForm.getPage(), 10, request.getRequestURI(), request.getQueryString());
            int firstIndex = (pageForm.getPage() - 1) * pageView.getMaxresult();

            QueryResult<Like> qr = likeService.findLikePageByTopicId(firstIndex, pageView.getMaxresult(), topicId);
            if (qr != null && qr.getResultlist() != null && qr.getResultlist().size() > 0) {
                for (Like like : qr.getResultlist()) {
                    User user = userManage.query_cache_findUserByUserName(like.getUserName());
                    if (user != null) {
                        like.setNickname(user.getNickname());
                        if (user.getAvatarName() != null && !"".equals(user.getAvatarName().trim())) {
                            like.setAvatarPath(fileManage.fileServerAddress() + user.getAvatarPath());
                            like.setAvatarName(user.getAvatarName());
                        }
                    }
                }
            }
            //将查询结果集传给分页List
            pageView.setQueryResult(qr);
            returnValue.put("pageView", pageView);
        } else {
            error.put("topicId", "话题Id不能为空");
        }
        if (error.size() > 0) {
            return JsonUtils.toJSONString(new RequestResult(ResultCode.FAILURE, error));
        } else {
            return JsonUtils.toJSONString(new RequestResult(ResultCode.SUCCESS, returnValue));
        }
    }
}
